<?php

namespace common\helpers;

use Yii;

class Data
{
    public static function cache($key, $duration, $callable)
    {
        $cache = Yii::$app->cache;
        if ($cache->exists($key))
        {
            $data = $cache->get($key);
        }
        else
        {
            $data = $callable();

            if ($data)
            {
                $cache->set($key, $data, $duration);
            }
        }
        return $data;
    }

    /**
     * 获取当前Yii设置的语言
     *
     * @return string
     */
    public static function getLocale()
    {
        return Yii::$app->language;
    }
}
